using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System;

namespace AuditionApp.Pages.QuestionBank
{
    public partial class ChildrenTemplatePage : Page
    {
        private string? _currentContent;

        public ChildrenTemplatePage()
        {
            InitializeComponent();
        }

        public void LoadQuestion(string title, string content, string analysis, string answer, string? source = null)
        {
            try
            {
                QuestionTitle.Text = title;
                _currentContent = content;
                LoadContent();
                LoadFormattedText(AnalyseContent, analysis);
                LoadFormattedText(AnswerContent, answer);
                if (!string.IsNullOrEmpty(source))
                {
                    SourceText.Text = $"来源：{source}";
                    SourceText.Visibility = Visibility.Visible;
                }
                else
                {
                    SourceText.Visibility = Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载问题内容时出错: {ex.Message}\n{ex.StackTrace}");
            }
        }

        private void LoadContent()
        {
            try
            {
                // Clear existing content
                QuestionContent.Document.Blocks.Clear();
                
                if (string.IsNullOrEmpty(_currentContent))
                {
                    return;
                }

                // Split content by newlines and create paragraphs
                string[] lines = _currentContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    string trimmedLine = line.Trim();
                    if (!string.IsNullOrWhiteSpace(trimmedLine))
                    {
                        // 移除开头的破折号和空格
                        if (trimmedLine.StartsWith("-"))
                        {
                            trimmedLine = trimmedLine.Substring(1).Trim();
                        }
                        
                        // Create a new paragraph with the line
                        var paragraph = new Paragraph(new Run(trimmedLine));
                        paragraph.Margin = new Thickness(0, 0, 0, 10);
                        QuestionContent.Document.Blocks.Add(paragraph);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载问题内容时出错: {ex.Message}\n{ex.StackTrace}");
            }
        }

        private void LoadFormattedText(TextBlock textBlock, string text)
        {
            try
            {
                textBlock.Inlines.Clear();
                
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }
                
                // 处理 """ 标记的文本块
                if (text.StartsWith("\"\"\"") && text.EndsWith("\"\"\""))
                {
                    // 移除首尾的 """
                    text = text.Substring(3, text.Length - 6);
                }
                
                // 使用 ** 来标记需要加粗的文本
                var parts = text.Split(new[] { "**" }, System.StringSplitOptions.None);
                for (int i = 0; i < parts.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        // 普通文本
                        textBlock.Inlines.Add(new Run(parts[i]));
                    }
                    else
                    {
                        // 加粗文本
                        textBlock.Inlines.Add(new Run(parts[i]) { FontWeight = FontWeights.Bold });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载格式化文本时出错: {ex.Message}\n{ex.StackTrace}");
            }
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
    }
} 
